#include<iostream>
#include<sys/types.h>
#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>
#include"managerdao_file_impl.h"

using namespace std;


vector<Manager>& ManagerDaoFilelmpl::load(vector<Manager>& managers)
{
	
	int fd = open("./account/managers.dat",O_RDONLY,0644);
	if(0 > fd)
	{
		perror("open");
	}
	while(1)
	{
		int _id;
		int ret= read(fd,&_id,sizeof(int));
		
		if(ret <= 0)
		{
			break;
		}

		char _name[20];
		int ret1 = read(fd,_name,20);
		
		if(ret1 <= 0)
		{
			break;
		}
		
		char _password[20];
		int ret2 = read(fd,_password,20);
	
		
		
		if(ret2 <= 0)
		{
			break;
		}
		Manager manager(_id,_name,_password);
		managers.push_back(manager);					
	}
	return managers;
}
void ManagerDaoFilelmpl::save(vector<Manager>& manager)
{
	int fd = open("./account/managers.dat",O_WRONLY,0644);
	for(unsigned int i =0; i<manager.size(); i++)
	{
		int _id;
		_id = manager[i].get_manager_id();
		write(fd,&_id,sizeof(int));
		write(fd,manager[i].get_manager_name(),20);
		write(fd,manager[i].get_manager_password(),20);
	}

}
